import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/utils/image_compress_util.dart';
import 'package:flutter_changdian_football/widget/sheet/param_picker.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:dio/dio.dart' as dio;

class FeedbackController extends GetxController {

  String feedBackType = '';
  List<String> feedBackList = ['系统BUG反馈', '功能需求', '优化建议', '商务合作', '其他'];
  int selectIndex = 0;

  /// 选择反馈类型
  changeFeedBackType() {
    FocusScope.of(Get.context!).requestFocus(FocusNode());
    ParamPicker.show(
      '反馈类型', 
      Get.context, 
      feedBackList,
      selecteds: selectIndex,
      onConfirm: (i) {
        selectIndex = i;
        feedBackType = feedBackList[i];
        update();
      }
    );
  }

  TextEditingController decorationController = TextEditingController();

  int inputLength = 0;

  List selectImage = [];

  photoSelectImage() async {
    Get.back();
    final PickedFile? image = await ImagePicker().getImage(source: ImageSource.gallery);

    if (image != null) {
      addImage(image.path);
    }
  }

  shootSelectImage() async {
    Get.back();
    final PickedFile? photo = await ImagePicker().getImage(source: ImageSource.camera);
    if (photo != null) {
      addImage(photo.path);
    }
  }

  addImage(path) async {
    try {
      EasyLoading.show();
      File imageCompressedImage = await ImageCompressUtil().imageCompressAndGetFile(File(path));

      dio.FormData formData = dio.FormData.fromMap(
        {
          'file': await dio.MultipartFile.fromFile(imageCompressedImage.path),
          'businessType': 'ADVICE'
        }
      );
      var resp = await ReqOther.fileUpload(formData);
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          selectImage.add(resp.data["data"]);
          update();
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

  delImage(index) {
    selectImage.removeAt(index);
    update();
  }

  submit() async {
    if (feedBackType == '') {
      CustomToast.showTextToast('请选择反馈类型');
      return ;
    }

    if (decorationController.text == '') {
      CustomToast.showTextToast('请输入您遇到的问题');
      return ;
    }

    try {
      EasyLoading.show();

      var resp = await ReqOther.userAdvice(
        adviceType: feedBackType,
        adviceContent: decorationController.text,
        firstImgUrl: selectImage.length >= 1 ? selectImage[0] : null,
        secondImgUrl: selectImage.length >= 2 ? selectImage[1] : null,
        thirdImgUrl: selectImage.length >= 3 ? selectImage[2] : null
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          CustomToast.showTextToast('反馈成功');
          Get.back();
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

}